const cmd = require("commander");//命令行工具
var shell = require('shelljs');
var chalk = require('chalk');
var pkgjson = require('../package.json')

var watch = require('watch');       //监测目录变化


// version
cmd.command('version').action(function () {
    console.log("version is " + chalk.green(pkgjson.version));
});


cmd.command('watch')
    .option('-u, --url <url>', '当文件发生变化，则会发送url')
    .usage(
        '\t观察当前文件夹内是否有变化，如果有变化，则发送url\n'
    )
    .action(function (cmd) {
        let url = cmd.url ? cmd.url : "http://localhost:9002/be-admin-srv/api/mcp/hot";

        let shellScript = `curl ${url}`;
        watch.watchTree('./', function (f, curr, prev) {
            shell.exec(shellScript, { silent: true }, function (code, stdout, stderr) {
                if (code == 0) {
                    console.log('通知远程服务成功', stdout);
                } else {
                    console.log('通知远程服务失败', stderr);
                }
            });
        });
    });


cmd.parse(process.argv);